package org.clickframes.clipr.controller;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.clickframes.clipr.domain.App;
import org.clickframes.clipr.service.UploadService;
import org.clickframes.clipr.service.UploadUploadFormUploadOutcome;
import org.clickframes.clipr.service.UploadService.UploadFormUploadResponse;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;

/**
 * Glue code to bind Upload an appspec Service with the view framework (Seam)
 * 
 * On this page, the user may upload an appspec.
 * 
 * Automatically generated by clickframes
 * 
 * @author Vineet Manohar
 */
@Name("uploadController")
@Scope(ScopeType.CONVERSATION)
public class UploadController extends GeneratedUploadController {
    @SuppressWarnings("unused")
    private final Log logger = LogFactory.getLog(getClass());

    @In("#{uploadService}")
    private UploadService uploadService;

    private UploadFormUploadResponse uploadFormUploadResponse;

    /**
     * Upload
     * 
     * Automatically generated by clickframes
     */
    @Override
    public UploadUploadFormUploadOutcome uploadFormUpload() {
        try {
            File file = File.createTempFile("clipr-uploaded", ".xml");

            // write bytes to file
            FileUtils.writeByteArrayToFile(file, super.getUploadFormYourAppspec());

            this.uploadFormUploadResponse = uploadService.uploadFormUpload(file);

            // store response, if persisting to session is needed
            return uploadFormUploadResponse.getOutcome();
        } catch (IOException ex) {
            throw new RuntimeException("Error uploading file", ex);
        }
    }

    private static final long serialVersionUID = 1L;

    public UploadFormUploadResponse getUploadFormUploadResponse() {
        return uploadFormUploadResponse;
    }

    public void setUploadFormUploadResponse(UploadFormUploadResponse uploadFormUploadResponse) {
        this.uploadFormUploadResponse = uploadFormUploadResponse;
    }
}
// clickframes::version=3618947232::clickframes
